<#assign s=JspTaglibs["/WEB-INF/struts-tags.tld"]>
<#assign sj=JspTaglibs["/WEB-INF/struts-jquery-tags.tld"]>

<#if parameters.type == "picker">

 <@s.hidden name="${parameters.name?html}" id="${parameters.id?html}"/>

<select id="${parameters.id?html}_mark_temp" onchange="${parameters.id?html}_changeMark()" style="width:50px;">
<option value=""></option>
</select>
<select id="${parameters.id?html}_year_temp" onchange="${parameters.id?html}_changeYear()" style="width:46px;">
<option value=""></option>
</select>年
<select id="${parameters.id?html}_month_temp" onchange="${parameters.id?html}_changeMonth()" style="width:46px;">
<option value=""></option>
</select>月
<select id="${parameters.id?html}_day_temp" onchange="${parameters.id?html}_changeDay()" style="width:46px;">
<option value=""></option>
</select>日
<@sj.a id='${parameters.id?html}_setValuebutton'
		targets="json"
		indicator="${parameters.id?html}_indicator"
		onSuccessTopics="${parameters.id?html}_mark"
		formIds="${parameters.id?html}_setValueform"
		dataType="json"
		cssStyle="display:none;"
	>
	  	Run AJAX Action
	</@sj.a>
	<#if parameters.showAge == "true">
		<div class="pickAge">
			西暦<span id="${parameters.id?html}_ageYear"></span>年生
			<span id="${parameters.id?html}_age" style="padding-left: 50px;"></span>歳
			&nbsp;&nbsp;<span id="${parameters.id?html}_ageM"></span>ヶ月
		</div>
	</#if>
<#elseif parameters.type == "driver">

<script language='javascript'>
    $.subscribe('${parameters.id?html}_mark', function(event,data) {
    	$('#${parameters.id?html}').val(event.originalEvent.data.waraki.seirekiDate);

    	<#if parameters.showAge == "true">
    		${parameters.id?html}_doAge();
    	</#if>

    	<#if parameters.defaultTime == "empty">
    		//alert($('#${parameters.id?html}').val());
    	</#if>
   	 	${parameters.id?html}_setSelectWl(event.originalEvent.data.warakiList,"${parameters.id?html}_mark_temp");

   	 	${parameters.id?html}_setSelect(event.originalEvent.data.years,"${parameters.id?html}_year_temp");
   		${parameters.id?html}_setSelect(event.originalEvent.data.months,"${parameters.id?html}_month_temp");
   	 	${parameters.id?html}_setSelect(event.originalEvent.data.days,"${parameters.id?html}_day_temp");

   	 	${parameters.id?html}_setSelect(event.originalEvent.data.years,"${parameters.id?html}_year");
   		${parameters.id?html}_setSelect(event.originalEvent.data.months,"${parameters.id?html}_month");
   	 	${parameters.id?html}_setSelect(event.originalEvent.data.days,"${parameters.id?html}_day");

		$('#${parameters.id?html}_mark_temp').val(event.originalEvent.data.waraki.mark);
		$('#${parameters.id?html}_mark').val(event.originalEvent.data.waraki.mark);
		$('#${parameters.id?html}_year_temp').val(event.originalEvent.data.waraki.year);
		$('#${parameters.id?html}_year').val(event.originalEvent.data.waraki.year);
		$('#${parameters.id?html}_month_temp').val(event.originalEvent.data.waraki.month);
		$('#${parameters.id?html}_month').val(event.originalEvent.data.waraki.month);
		$('#${parameters.id?html}_day_temp').val(event.originalEvent.data.waraki.day);
		$('#${parameters.id?html}_day').val(event.originalEvent.data.waraki.day);

    });

    $.subscribe('${parameters.id?html}_year', function(event,data) {
    	$('#${parameters.id?html}').val(event.originalEvent.data.waraki.seirekiDate);
    	<#if parameters.showAge == "true">
    		${parameters.id?html}_doAge();
    	</#if>

		${parameters.id?html}_setSelect(event.originalEvent.data.years,"${parameters.id?html}_year_temp");
    	${parameters.id?html}_setSelect(event.originalEvent.data.months,"${parameters.id?html}_month_temp");
    	${parameters.id?html}_setSelect(event.originalEvent.data.days,"${parameters.id?html}_day_temp");

    	${parameters.id?html}_setSelect(event.originalEvent.data.months,"${parameters.id?html}_month");
    	${parameters.id?html}_setSelect(event.originalEvent.data.days,"${parameters.id?html}_day");
    });

    $.subscribe('${parameters.id?html}_month', function(event,data) {
    	$('#${parameters.id?html}').val(event.originalEvent.data.waraki.seirekiDate);
    	<#if parameters.showAge == "true">
    		${parameters.id?html}_doAge();
    	</#if>

		${parameters.id?html}_setSelect(event.originalEvent.data.months,"${parameters.id?html}_month_temp");
    	${parameters.id?html}_setSelect(event.originalEvent.data.days,"${parameters.id?html}_day_temp");

    	${parameters.id?html}_setSelect(event.originalEvent.data.days,"${parameters.id?html}_day");
    });

   $.subscribe('${parameters.id?html}_day', function(event,data) {
    	$('#${parameters.id?html}').val(event.originalEvent.data.waraki.seirekiDate);
    	<#if parameters.showAge == "true">
    		${parameters.id?html}_doAge();
    	</#if>
    	${parameters.id?html}_setSelect(event.originalEvent.data.days,"${parameters.id?html}_day_temp");
    });


	function ${parameters.id?html}_changeData(){
	     $('#${parameters.id?html}').val($('#${parameters.id?html}').val().substring(0,8)+$('#${parameters.id?html}_day').val());
	     <#if parameters.showAge == "true">
    		${parameters.id?html}_doAge();
    	</#if>
	    }

	function ${parameters.id?html}_setSelect(data,type){
	  $("#"+type).empty();
	  $.each(data, function(index, value) {
	      if(index==0){
	        $("<option value='"+value+"' selected='selected'>"+value+"</option>").appendTo('#'+type);
	      }else{
	        $("<option value='"+value+"'>"+value+"</option>").appendTo('#'+type);
	      }
		});
	}
		function ${parameters.id?html}_setSelectWl(data,type){
	  $("#"+type).empty();
	  $.each(data, function(index, value) {
	      if(index==0){
	        $("<option value='"+value.mark+"' selected='selected'>"+value.name+"</option>").appendTo('#'+type);
	      }else{
	        $("<option value='"+value.mark+"'>"+value.name+"</option>").appendTo('#'+type);
	      }
		});
	}

		function ${parameters.id?html}_changeMark(){
	      $('#${parameters.id?html}_mark').val($('#${parameters.id?html}_mark_temp').val());
	      $('#${parameters.id?html}_mark').change();
	    }
		function ${parameters.id?html}_changeYear(){
	      $('#${parameters.id?html}_year').val($('#${parameters.id?html}_year_temp').val());
	      $('#${parameters.id?html}_year').change();
	    }
	    function ${parameters.id?html}_changeMonth(){
	      $('#${parameters.id?html}_month').val($('#${parameters.id?html}_month_temp').val());
	      $('#${parameters.id?html}_month').change();
	    }
	    function ${parameters.id?html}_changeDay(){
	      $('#${parameters.id?html}_day').val($('#${parameters.id?html}_day_temp').val());
	      $('#${parameters.id?html}_day').change();
	    }

<#if parameters.showAge == "true">
	    function ${parameters.id?html}_doAge(){
	      var date= $('#${parameters.id?html}').val();
	      if(date!=""){
		      var year=${datetime("yyyy")}-date.substring(0, 4);
		      var month=${datetime("M")}-date.substring(5, 7);
		      var day=${datetime("dd")}-date.substring(8, 10);
		      if(month<0){
		        month=month+12;
		        year=year-1;
		      }
		      if(day<-1){
		      	month=month-1;
		      	if(month<0){
		        month=month+12;
		        year=year-1;
		      }
		      }
			  $('#${parameters.id?html}_ageYear').empty();
		      $('#${parameters.id?html}_ageYear').append(date.substring(0, 4));
		      $('#${parameters.id?html}_age').empty();
		      $('#${parameters.id?html}_age').append(year);
		      $('#${parameters.id?html}_ageM').empty();
		      $('#${parameters.id?html}_ageM').append(month);
	        }
	    }
</#if>

function ${parameters.id?html}_setValue(date){
	      $('#${parameters.id?html}_defaultTime').val(date);
	      $('#${parameters.id?html}_setValuebutton').click();
	    }

    </script>

<#assign str="false"/>

<#if parameters.haveEmptyValue?if_exists != "" && parameters.haveEmptyValue?html="true">
          <#assign str="true"/>
  </#if>

<#assign defaultTimeStr=parameters.defaultTime/>

 <#if stack.findValue(parameters.name)?exists>
  <#assign defaultTimeStr=stack.findValue(parameters.name)?string("yyyy/MM/dd")/>
 </#if>
<script language='javascript'>

$(document).ready(function() {
$("body").append("<form id='${parameters.id?html}_setValueform' action='warekipick.do'>"+
		                 "<input id='${parameters.id?html}_haveEmptyValue' type='hidden' value='${str}' name='haveEmptyValue' />"+
		                  "<input id='${parameters.id?html}_defaultTime' type='hidden' value='20101202' name='defaultTime' />"+
		                 "</form>");
    });

    </script>

        <@s.form id="${parameters.id?html}_pickform" >
    <@s.url id="${parameters.id?html}_change" action="warekipick.do?haveEmptyValue=${str}&defaultTime=${defaultTimeStr}">
      </@s.url>
     <@sj.select  id="${parameters.id?html}_mark"
     name="waraki.mark"
     list="warakiList"
     listKey="mark"
     listValue="name"
     href="%{${parameters.id?html}_change}"
     onChangeTopics="${parameters.id?html}_reloadMark"
     onSuccessTopics="${parameters.id?html}_mark"
     formIds="${parameters.id?html}_pickform"
      cssStyle="display:none;"
     indicator="${parameters.id?html}_indicator"
     onErrorTopics="timeout"
     >
    </@sj.select>

 <@s.url id="${parameters.id?html}_changeYear" action="warekipick!changeYear.do" ></@s.url>
   <@sj.select  id="${parameters.id?html}_year"
     name="waraki.year"
     list="years"
     href="%{${parameters.id?html}_changeYear}"
     reloadTopics="${parameters.id?html}_reloadMark"
     onChangeTopics="${parameters.id?html}_reloadYear"
     onSuccessTopics="${parameters.id?html}_year"
     deferredLoading="true"
     formIds="${parameters.id?html}_pickform"
      cssStyle="display:none;"
      indicator="${parameters.id?html}_indicator"
      onErrorTopics="timeout"
     >
     </@sj.select>

      <@s.url id="${parameters.id?html}_changeMonth" action="warekipick!changeMonth.do" ></@s.url>
   <@sj.select  id="${parameters.id?html}_month"
     name="waraki.month"
     list="months"
     href="%{${parameters.id?html}_changeMonth}"
     reloadTopics="${parameters.id?html}_reloadYear"
     onChangeTopics="${parameters.id?html}_reloadMonth"
     onSuccessTopics="${parameters.id?html}_month"
     deferredLoading="true"
     formIds="${parameters.id?html}_pickform"
      cssStyle="display:none;"
      indicator="${parameters.id?html}_indicator"
      onErrorTopics="timeout"
     >
     </@sj.select>

   <@s.url id="${parameters.id?html}_findDay" action="warekipick!findDay.do" ></@s.url>
   <@sj.select  id="${parameters.id?html}_day"
     name="waraki.day"
     list="days"
     href="%{${parameters.id?html}_findDay}"
     deferredLoading="true"
     reloadTopics="${parameters.id?html}_reloadMonth"
     onSuccessTopics="${parameters.id?html}_day"
     onchange="${parameters.id?html}_changeData()"
     formIds="${parameters.id?html}_pickform"
      cssStyle="display:none;"
      indicator="${parameters.id?html}_indicator"
      onErrorTopics="timeout"
     >
     </@sj.select>

   </@s.form>
   </#if>